Java JavaScript Python C# C C++ Go Kotlin PHP Swift R Ruby TypeScript Scala SQL Perl rust VisualBasic Matlab Julia

Inheritance → Python super()

Inheritance

Python super()

The `super()` function in Python is a powerful tool for working with inheritance, especially when dealing with multiple inheritance. It allows a subclass to call methods of its parent (or super) classes, enabling code reuse and ensuring method chaining operates correctly. Let's break down its functionality with detailed examples.

Understanding Inheritance and Method Resolution Order (MRO)

Before diving into `super()`, let's refresh inheritance. In Python, a class can inherit attributes and methods from another class (its parent or superclass). When a subclass overrides a method from its parent class, it might still need to access the parent's version of that method. This is where `super()` comes in. Python's MRO determines the order in which methods are looked up during inheritance. It follows the C3 linearization algorithm to ensure consistent and predictable inheritance behavior, especially crucial in multiple inheritance scenarios.

Basic `super()` Usage (Single Inheritance)

Let's consider a simple example with single inheritance:
Basic `super()` Usage (Single Inheritance) class Animal: def __init__(self, name): self.name = name print(f"Animal {self.name} created") def speak(self): print("Generic animal sound") class Dog(Animal): def __init__(self, name, breed): super().__init__(name) # Calls Animal's __init__ self.breed = breed print(f"Dog {self.name} ({self.breed}) created") def speak(self): print("Woof!") my_dog = Dog("Buddy", "Golden Retriever") my_dog.speak()

Output

Animal Buddy created Dog Buddy (Golden Retriever) created Woof!
Here, `Dog` inherits from `Animal`. `super().__init__(name)` in `Dog`'s `__init__` explicitly calls the `__init__` method of the `Animal` class, initializing the `name` attribute. Without `super()`, the `name` attribute wouldn't be set correctly. Similarly, `Dog` overrides `speak()`, showcasing method overriding. Calling `my_dog.speak()` prints "Woof!" instead of the generic animal sound.

`super()` with Multiple Inheritance

Multiple inheritance is where things get more interesting. Let's see how `super()` handles it:
`super()` with Multiple Inheritance in python class Flyer: def fly(self): print("Flying!") class Swimmer: def swim(self): print("Swimming!") class FlyingFish(Flyer, Swimmer): # Inherits from Flyer and Swimmer def __init__(self, name): super().__init__() # Calls the __init__ of the first parent class (Flyer - which doesn't have one) self.name = name print(f"Flying Fish {self.name} created") my_fish = FlyingFish("Finny") my_fish.fly() my_fish.swim()

Output

Flying Fish Finny created Flying! Swimming!
In this example, `FlyingFish` inherits from both `Flyer` and `Swimmer`. The MRO ensures that `Flyer`'s methods are checked before `Swimmer`'s. Note that if `Flyer` had an `__init__` method, it would be called first by `super().__init__()`. If `Flyer`'s `__init__` required parameters, you would need to pass them appropriately: `super().__init__(parameters)`. This demonstrates how `super()` handles method resolution in multiple inheritance scenarios.

`super()` and Method Chaining

`super()` is crucial for proper method chaining, especially in scenarios where multiple inherited classes have methods with the same name:
Python - `super()` and Method Chaining class A: def method(self): print("Method A") class B(A): def method(self): super().method() print("Method B") class C(A): def method(self): print("Method C") class D(B,C): def method(self): super().method() print("Method D") d = D() d.method()

Output

Method C Method B Method D
Here, `D` inherits from both `B` and `C`. Because of MRO, the call to `super().method()` in `D` first calls `C.method()` and then `B.method()` before printing "Method D".

`super()` without arguments

When you call `super()` without explicit arguments, Python implicitly provides the necessary arguments (usually `self`). However, it is good practice to provide parameters if the superclass method's constructor takes them. In summary The `super()` function is vital for writing clean, maintainable, and correctly functioning code with inheritance in Python. It simplifies the process of interacting with parent classes, ensuring that method overriding and chaining work as intended, especially in complex multiple inheritance scenarios. Understanding MRO is key to effectively using `super()` and avoiding unexpected behavior.

Tutorials